﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Microsoft.Win32;

namespace PVFSLEAP
{
	public partial class F_SETT: Form
	{
		private string INI = "..\\..\\ENV.ini";

		// [ Sensor Field ] -----------------------------------------------------------------------
		public	int	TTND;
		public	int RANG;
		public	int CHLD;
		public	int BSPX;
		public	int BSPY;
		public	int GNTC;

		// [ Attack ] ----------------------------------------------------------------------------
		public	int ATYP;
		public	int FRIA;
		public	int WORM;
		public	int SKHC;

		// [ Defense ] -----------------------------------------------------------------------------
		public	int	DTYP;
		public	int	PVFS;
		public	int LEAP;
		public	int	PPMT;

		// [ Event ] ------------------------------------------------------------------------------
		public	int AQOE;
		public	int CNFV;
		public	int FRIR;
		
		// [ PVFS ] --------------------------------------------------------------------------------
		public	int SPID;
		public	int SKEY;
		public	int KYDS;
		public	int VOTE;

		public	int	TVFV;

		//Total NODE 다른 클래스에서도 사용할 수 있도록 from F_SETT

		public F_SETT()
		{
			InitializeComponent();

			StringBuilder S_rslt = new StringBuilder( 16 );

			// [ Sensor Field ] -------------------------------------------------------------------
			C_INIF.GetPrivateProfileString( "SensorField", "TTND", "error", S_rslt, 16, INI );
			TB_TTND.Text = S_rslt.ToString();
			C_INIF.GetPrivateProfileString( "SensorField", "CHLD", "error", S_rslt, 16, INI );
			TB_CHLD.Text = S_rslt.ToString();
			C_INIF.GetPrivateProfileString( "SensorField", "BSPX", "error", S_rslt, 16, INI );
			TB_BSPX.Text = S_rslt.ToString();
			C_INIF.GetPrivateProfileString( "SensorField", "BSPY", "error", S_rslt, 16, INI );
			TB_BSPY.Text = S_rslt.ToString();
			C_INIF.GetPrivateProfileString( "SensorField", "RANG", "error", S_rslt, 16, INI );
			TB_RANG.Text = S_rslt.ToString();
			C_INIF.GetPrivateProfileString( "SensorField", "GNTC", "error", S_rslt, 16, INI );
			CB_GNTC.Checked = SetCBValue( S_rslt.ToString() );

			// [ Attack ] -------------------------------------------------------------------------
			C_INIF.GetPrivateProfileString( "Attack", "TYPE", "error", S_rslt, 16, INI );
			CB_ATYP.Checked = SetCBValue( S_rslt.ToString() );
			C_INIF.GetPrivateProfileString( "Attack", "FRIA", "error", S_rslt, 16, INI );
			CB_FRIA.Checked = SetCBValue( S_rslt.ToString() );
			C_INIF.GetPrivateProfileString( "Attack", "WORM", "error", S_rslt, 16, INI );
			
			MakeStateATYP( CB_ATYP.Checked );

			// [ Defense ] ------------------------------------------------------------------------
			C_INIF.GetPrivateProfileString( "Defense", "TYPE", "error", S_rslt, 16, INI );
			CB_DTYP.Checked = SetCBValue( S_rslt.ToString() );
			C_INIF.GetPrivateProfileString( "Defense", "PVFS", "error", S_rslt, 16, INI );
			CB_PVFS.Checked = SetCBValue( S_rslt.ToString() );
			C_INIF.GetPrivateProfileString( "Defense", "PPMT", "error", S_rslt, 16, INI );
			
			CB_PPMT.Checked = SetCBValue( S_rslt.ToString() );

			MakeStateDTYP( CB_DTYP.Checked );

			// [ Event ] --------------------------------------------------------------------------
			C_INIF.GetPrivateProfileString( "Event", "AQOE", "error", S_rslt, 16, INI );
			TB_AQOE.Text = S_rslt.ToString();
			C_INIF.GetPrivateProfileString( "Event", "CNFV", "error", S_rslt, 16, INI );
			TB_CNFV.Text = S_rslt.ToString();
			C_INIF.GetPrivateProfileString( "Event", "FRIR", "error", S_rslt, 16, INI );
			TB_FRIR.Text = S_rslt.ToString();


			// [ PVFS ] ----------------------------------------------------------------------------
			C_INIF.GetPrivateProfileString( "PVFS", "SPID", "error", S_rslt, 16, INI );
			TB_SPID.Text = S_rslt.ToString();
			C_INIF.GetPrivateProfileString( "PVFS", "SKEY", "error", S_rslt, 16, INI );
			TB_SKEY.Text = S_rslt.ToString();
			C_INIF.GetPrivateProfileString( "PVFS", "KYDS", "error", S_rslt, 16, INI );
			TB_KYDS.Text = S_rslt.ToString();
			C_INIF.GetPrivateProfileString( "PVFS", "VOTE", "error", S_rslt, 16, INI );
			TB_VOTE.Text = S_rslt.ToString();
			C_INIF.GetPrivateProfileString( "PVFS", "TVFV", "error", S_rslt, 16, INI );
			TB_TVFV.Text = S_rslt.ToString();

			SetValues();
		}

		private void BT_OKAY_Click( object sender, EventArgs e )
		{
			// [ Sesnro Field ] -------------------------------------------------------------------
			C_INIF.WritePrivateProfileString( "SensorField", "TTND", TB_TTND.Text, INI );
			C_INIF.WritePrivateProfileString( "SensorField", "BSPX", TB_BSPX.Text, INI );
			C_INIF.WritePrivateProfileString( "SensorField", "BSPY", TB_BSPY.Text, INI );
			C_INIF.WritePrivateProfileString( "SensorField", "RANG", TB_RANG.Text, INI );
			C_INIF.WritePrivateProfileString( "SensorField", "GNTC", GetCBValue( CB_GNTC.Checked ), INI );

			// [ Attack ] -------------------------------------------------------------------------
			C_INIF.WritePrivateProfileString( "Attack", "TYPE", GetCBValue( CB_ATYP.Checked ), INI );
			C_INIF.WritePrivateProfileString( "Attack", "FRIA", GetCBValue( CB_FRIA.Checked ), INI );

			// [ Defense ] ------------------------------------------------------------------------
			C_INIF.WritePrivateProfileString( "Defense", "TYPE", GetCBValue( CB_DTYP.Checked ), INI );
			C_INIF.WritePrivateProfileString( "Defense", "PVFS", GetCBValue( CB_PVFS.Checked ), INI );
			C_INIF.WritePrivateProfileString( "Defense", "PPMT", GetCBValue( CB_PPMT.Checked ), INI );

			// [ Event ] --------------------------------------------------------------------------
			C_INIF.WritePrivateProfileString( "Event", "AQOE", TB_AQOE.Text, INI );
			C_INIF.WritePrivateProfileString( "Event", "CNFV", TB_CNFV.Text, INI );
			C_INIF.WritePrivateProfileString( "Event", "FRIR", TB_FRIR.Text, INI );

			// [ PVFS ] ----------------------------------------------------------------------------
			C_INIF.WritePrivateProfileString( "PVFS", "SPID", TB_SPID.Text, INI );
			C_INIF.WritePrivateProfileString( "PVFS", "SKEY", TB_SKEY.Text, INI );
			C_INIF.WritePrivateProfileString( "PVFS", "KYDS", TB_KYDS.Text, INI );
			C_INIF.WritePrivateProfileString( "PVFS", "VOTE", TB_VOTE.Text, INI );
			C_INIF.WritePrivateProfileString( "PVFS", "TVFV", TB_TVFV.Text, INI );

			// [ LEAP ]
			

			SetValues();

			Close();
		}

		public void SetValues()
		{
			// [ Sesnro Field ] -------------------------------------------------------------------
			OTTND = int.Parse( TB_TTND.Text );
			OBSPX = int.Parse( TB_BSPX.Text );
			OBSPY = int.Parse( TB_BSPY.Text );
			ORANG = int.Parse( TB_RANG.Text );
			OCHLD = int.Parse( TB_CHLD.Text );
			OGNTC = ConvertCBValue( CB_GNTC.Checked );

			// [ Attack ] -------------------------------------------------------------------------
			OATYP = ConvertCBValue( CB_ATYP.Checked );
			OFRIA = ConvertCBValue( CB_FRIA.Checked );

			// [ Defense ] ------------------------------------------------------------------------
			ODTYP = ConvertCBValue( CB_DTYP.Checked );
			OPVFS = ConvertCBValue( CB_PVFS.Checked );
			OPPMT = ConvertCBValue( CB_PPMT.Checked );			

			// [ Event ] --------------------------------------------------------------------------
			OAQOE = int.Parse( TB_AQOE.Text );
			OCNFV = int.Parse( TB_CNFV.Text );
			OFRIR = int.Parse( TB_FRIR.Text );

			// [ PVFS ] ----------------------------------------------------------------------------
			OSPID = int.Parse( TB_SPID.Text );
			OSKEY = int.Parse( TB_SKEY.Text );
			OKYDS = int.Parse( TB_KYDS.Text );
			OVOTE = int.Parse( TB_VOTE.Text );
			OTVFV = int.Parse( TB_TVFV.Text );
		}

		public string GetCBValue( bool V )
		{
			if ( V == false )
				return "0";
			else
				return "1";
		}

		public int ConvertCBValue( bool V )
		{
			if ( V == false )
				return 0;
			else
				return 1;
		}

		public bool SetCBValue( string V )
		{
			if ( V == "1" )
				return true;
			else
				return false;
		}		

		// [ Sensor Field ] -----------------------------------------------------------------------
		public int OTTND
		{
			get { return TTND;	}
			set	{ TTND = value;	}
		}

		public int OCHLD
		{
			get { return CHLD; }
			set { CHLD = value; }
		}

		public int ORANG
		{
			get { return RANG; }
			set { RANG = value; }
		}

		public int OBSPX
		{
			get { return BSPX; }
			set { BSPX = value; }
		}

		public int OBSPY
		{
			get { return BSPY; }
			set { BSPY = value; }
		}

		public int OGNTC
		{
			get { return GNTC; }
			set { GNTC = value; }
		}

		// [ Attack ] ----------------------------------------------------------------------------
		public int OATYP
		{
			get { return ATYP; }
			set { ATYP = value; }
		}

		public int OFRIA
		{
			get { return FRIA; }
			set { FRIA = value; }
		}

		public int OWORM
		{
			get { return WORM; }
			set { WORM = value; }
		}

		public int OSKHC
		{
			get { return SKHC; }
			set { SKHC = value; }
		}

		// [ Defense ] ----------------------------------------------------------------------------
		public int ODTYP
		{
			get { return DTYP; }
			set { DTYP = value; }
		}

		public int OPVFS
		{
			get { return PVFS; }
			set { PVFS = value; }
		}

		public int OLEAP
		{
			get { return LEAP; }
			set { LEAP = value; }
		}

		public int OPPMT
		{
			get { return PPMT; }
			set { PPMT = value; }
		}

		// [ Event ] ------------------------------------------------------------------------------
		public int OAQOE
		{
			get { return AQOE; }
			set { AQOE = value; }
		}

		public int OCNFV
		{
			get { return CNFV; }
			set { CNFV = value; }
		}

		public int OFRIR
		{
			get { return FRIR; }
			set { FRIR = value; }
		}

		// [ SEF ] --------------------------------------------------------------------------------
		public int OSPID
		{
			get { return SPID; }
			set { SPID = value; }
		}

		public int OSKEY
		{
			get { return SKEY; }
			set { SKEY = value; }
		}

		public int OKYDS
		{
			get { return KYDS; }
			set { KYDS = value; }
		}

		public int OVOTE
		{
			get { return VOTE; }
			set { VOTE = value; }
		}

		public int OTVFV
		{
			get { return TVFV; }
			set { TVFV = value; }
		}

		private void TB_WDTH_KeyPress( object sender, KeyPressEventArgs e )
		{
			if ( !char.IsNumber(e.KeyChar) && e.KeyChar != 8 )
				e.Handled = true;
		}

		private void CB_ATYP_Click( object sender, EventArgs e )
		{
			string name = ( ( CheckBox )sender ).Name.ToString();
			string CBNM = name.Substring( 3 );

			switch ( CBNM )
			{
				case "ATYP":
					MakeStateATYP( CB_ATYP.Checked );
					break;
				case "DTYP":
					MakeStateDTYP( CB_DTYP.Checked );
					break;
			}
		}

		private void MakeStateATYP( bool V )
		{
			if ( V == true )
			{
				CB_FRIA.Enabled = true;
			}
			else
			{
				CB_FRIA.Enabled = false;
			}
		}

		private void MakeStateDTYP( bool V )
		{
			if ( V == true )
			{
				CB_PVFS.Enabled = true;
				CB_PPMT.Enabled	= true;
			}
			else
			{
				CB_PVFS.Enabled = false;
				CB_PPMT.Enabled = false;
			}
		}
	}
}
